<?php

 /**
  * @package JooS
  * @subpackage PHPackager_Archive
  */

  require_once "JooS/PHPackager/Archive/Interface.php";

  class JooS_PHPackager_Archive implements JooS_PHPackager_Archive_Interface {
    private $_adapter = null;

    /**
     * @var ZipArchive
     */
    private $_zip = null;

    /**
     * @return JooS_PHPackager_Archive
     */
    public function create() {
      if (class_exists("ZipArchive") && $this->_zip === null) {
        $this->_zip = new ZipArchive();
        $this->_zip->open($this->getAdapter()->get_name(), ZipArchive::CREATE);
      }
      return $this;
    }

    /**
     * @param string $localName
     * @param string $content
     * @return JooS_PHPackager_Archive 
     */
    public function addFromString($localName, $content) {
      if ($this->_zip !== null) {
        $this->_zip->addFromString($localName, $content);
      }
      return $this;
    }

    /**
     * @param string $realPath
     * @param string $localName
     * @return JooS_PHPackager_Archive
     */
    public function addFile($realPath, $localName) {
      if ($this->_zip !== null) {
        $this->_zip->addFile($realPath, $localName);
      }
      return $this;
    }

    /**
     * @return JooS_PHPackager_Archive
     */
    public function close() {
      if ($this->_zip !== null) {
        $this->_zip->close();
      }
      return $this;
    }

    /**
     * @return JooS_CodeGenerator_File_Adapter
     */
    public function getAdapter() {
      if ($this->_adapter === null) {
        $this->_adapter = JooS_CodeGenerator_File::createAdapter("Tmp");
      }
      return $this->_adapter;
    }
  }
